var activityService = {
  getActivityList: function (options) {
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/activity/getActivityList',
      method: 'POST',
      data: util.signature({
        cityId: options.cityId,
        pageNumber: options.pageIndex,
        pageSize: options.pageSize,
        
        status: options.status ? options.status : 0
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function (a) {
        console.log(a);
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  getTeamAndVolunteerCount: function (options){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/activity/getTeamAndVolunteerCount/' + options.cityId,
      method: 'GET',
      data: util.signature({}),
      header: util.buildRequestheader(),
      success: function (res) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  getActivityDetail:function(opt){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/activity/activityDetail',
      method: 'POST',
      data: util.signature({
        id:opt.id
      }),
      header: util.buildRequestheader(),
      success: function (res) {
        if (res.data.Success) {
          opt.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(opt.failure, res);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  joinActivity:function(opt){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    if (util.checkPermission(this, opt)) {
      wx.request({
        url: app.getApiPathPrefix() + '/activity/joinActivityApply',
        method: 'POST',
        data: util.signature({
          city_id: opt.city_id,
          user_id: opt.user_id,
          activity_id: opt.activity_id
        }),
        header: util.buildRequestheader(),
        success: function (res) {
          if (res.data.Success) {
            opt.success.call(that, res.data.ResultData);
          } else if (res.statusCode == 422) {
            var errors = res.data
            var msg = '';

            for (var errorcode in errors) {
              var errorItem = errors[errorcode];
              console.log(errorItem)
              for (var i = 0; i < errorItem.length; i++) {
                msg += errorItem[i] + '　';
              }
            }
            wx.showToast({
              title: msg,
              icon: 'loading',
              duration: 2000
            });
          } else {
            util.processFailureRequest(opt.failure, res);
          }
        },
        fail: function () {
          wx.showToast({
            title: '服务器忙，请稍后重试',
            icon: 'loading',
            duration: 2000
          });
        }
      })
    }
  },

  getJoinActivity:function(opt){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/activity/getJoinActivity',
      method: 'POST',
      data: util.signature({
        user_id: opt.user_id
      }),
      header: util.buildRequestheader(),
      success: function (res) {
        if (res.data.Success) {
          opt.success.call(that, res.data.ResultData);
        } else if (res.statusCode == 422) {
          var errors = res.data
          var msg = '';

          for (var errorcode in errors) {
            var errorItem = errors[errorcode];
            console.log(errorItem)
            for (var i = 0; i < errorItem.length; i++) {
              msg += errorItem[i] + '　';
            }
          }
          wx.showToast({
            title: msg,
            icon: 'loading',
            duration: 2000
          });
        } else {
          util.processFailureRequest(opt.failure, res);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  activitySignIn:function(opt){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/activity/activitySign',
      method: 'POST',
      data: util.signature({
        id: opt.id
      }),
      header: util.buildRequestheader(),
      success: function (res) {
        if (res.data.Success) {
          opt.success.call(that, res.data.ResultData);
        } else if (res.statusCode == 422) {
          var errors = res.data
          var msg = '';

          for (var errorcode in errors) {
            var errorItem = errors[errorcode];
            console.log(errorItem)
            for (var i = 0; i < errorItem.length; i++) {
              msg += errorItem[i] + '　';
            }
          }
          wx.showToast({
            title: msg,
            icon: 'loading',
            duration: 2000
          });
        } else {
          util.processFailureRequest(opt.failure, res);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  activitySignOut:function(opt){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/activity/activitySignOut',
      method: 'POST',
      data: util.signature({
        id: opt.id
      }),
      header: util.buildRequestheader(),
      success: function (res) {
        if (res.data.Success) {
          opt.success.call(that, res.data.ResultData);
        } else if (res.statusCode == 422) {
          var errors = res.data
          var msg = '';

          for (var errorcode in errors) {
            var errorItem = errors[errorcode];
            console.log(errorItem)
            for (var i = 0; i < errorItem.length; i++) {
              msg += errorItem[i] + '　';
            }
          }
          wx.showToast({
            title: msg,
            icon: 'loading',
            duration: 2000
          });
        } else {
          util.processFailureRequest(opt.failure, res);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  getCity: function (opt){
    var util = require('../utils/util.js');
    var city = require('../utils/city.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/activity/getAllCity',
      method: 'POST',
      data: util.signature({ }),
      header: util.buildRequestheader(),
      success: function (res) {
        if (res.data.Success) {
          opt.success.call(that, res.data.ResultData);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },

  getCityName: function (opt) {
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/activity/getCityName',
      method: 'POST',
      data: util.signature({
        id : opt.id
      }),
      header: util.buildRequestheader(),
      success: function (res) {
        if (res.data.Success) {
          opt.success.call(that, res.data.ResultData);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  createActivity: function (opt) {
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/activity/createActivity',
      method: 'POST',
      data: util.signature(opt.form_data),
      header: util.buildRequestheader(),
      success: function (res) {
        if (res.data.Success) {
          opt.success.call(that, res.data.ResultData);
        } else if (res.statusCode == 422) {
          var errors = res.data
          var msg = '';

          for (var errorcode in errors) {
            var errorItem = errors[errorcode];
            for (var i = 0; i < errorItem.length; i++) {
              msg = errorItem[i]; 
              break;
            }
          }
          wx.showToast({
            title: msg,
            icon: 'loading',
            duration: 2000
          });
        } else {
          util.processFailureRequest(opt.failure, res);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  uploadImg:function(options){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.globalData.apiHost + '/backstage/api/upload',
      method: 'POST',
      data: util.signature({
        file: options.file[0].path
      }),
      header: util.buildRequestheader(),
      success: function (res) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  getNotesDetail: function (opt){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/Note/getNotesDetail',
      method: 'POST',
      data: util.signature({
        mid: opt.note_id,
        user_id: opt.user_id
      }),
      header: util.buildRequestheader(),
      success: function (res) {
        if (res.data.Success) {
          opt.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(opt.failure, res);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  getNotesList:function(opt){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/Note/getNotesList',
      method: 'POST',
      data: util.signature({
        user_id: opt.user_id,
        pageSize: opt.pageSize,
        pageNumber:opt.pageNum
      }),
      header: util.buildRequestheader(),
      success: function (res) {
        if (res.data.Success) {
          opt.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(opt.failure, res);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  }
};


module.exports = {
  activitySignIn: activityService.activitySignIn,
  activitySignOut: activityService.activitySignOut,
  getActivityList: activityService.getActivityList,
  getActivityDetail: activityService.getActivityDetail,
  joinActivity: activityService.joinActivity,
  getTeamAndVolunteerCount: activityService.getTeamAndVolunteerCount,
  getJoinActivity: activityService.getJoinActivity,
  getCity: activityService.getCity,
  getCityName: activityService.getCityName,
  createActivity: activityService.createActivity,
  uploadImg: activityService.uploadImg,
  getNotesList: activityService.getNotesList,
  getNotesDetail: activityService.getNotesDetail
}